﻿using CuocChienQueNha.Class.MapClass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace CuocChienQueNha.Class.Character
{
    public class Enemy
    {
        #region nơi đặt các biến
        int Type;//loại lính
        public bool isEnable { get; set; } // còn sống hay ko?
        public Vector2 position; // vị trí trên màn hình
        Vector2 size; // kích thước
        Vector2 sizeImages; // kích thước ảnh amination
        Vector2 dolechAnh;// do lech anh se dc ve
        public Texture2D image { get; set; }// ảnh load của địch
        public Vector2 index;// chỉ số đứng của địch trên ma trận
        int value;// tiền đc thưởng khi chết
        Vector2 indexRe;//ô đã xác định rẽ
        Map map;//để gọi các hàm để vẽ
        Assets asset;

        //biến phục vụ vẽ
        int count;

        #endregion
        public Enemy(int loai, Vector2 index, Assets asset)
        {
            dolechAnh = Vector2.Zero;
            map = new Map();
            this.Type = loai;
            this.index = index;
            this.position = new Vector2(index.Y, index.X) * 40;
            size = new Vector2(40, 40);
            isEnable = true;
            this.asset = asset;
            indexRe = Vector2.Zero;
        }

        public void SubBlood(float dame, CuocChienQueNha.Class.MapClass.Effect effect)
        {
            this.asset.AddEffect(effect);
        }

        public void setSizeimages()
        {
            dolechAnh = new Vector2(0, -5);
            switch (Type)
            {
                case 1:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(240, 40);
                        dolechAnh = new Vector2(0, -10);
                        break;
                        #endregion
                    }
                case 2:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(240, 52);
                        dolechAnh = new Vector2(0, -25);
                        break;
                        #endregion
                    }
                case 3:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(264, 44);
                        dolechAnh = new Vector2(-8, -25);
                        break;
                        #endregion
                    }
                case 4:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(336, 56);
                        dolechAnh = new Vector2(-11, -25);
                        break;
                        #endregion
                    }
                case 5:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(336, 50);
                        dolechAnh = new Vector2(-11, -25);
                        break;
                        #endregion
                    }
                case 6:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(336, 54);
                        dolechAnh = new Vector2(-11, -30);
                        break;
                        #endregion
                    }
                case 7:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(438, 73);
                        dolechAnh = new Vector2(-20, -45);
                        break;
                        #endregion
                    }
                case 8:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(510, 100);
                        dolechAnh = new Vector2(-25, -60);
                        break;
                        #endregion
                    }
                case 9:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(684, 100);
                        dolechAnh = new Vector2(-40, -65);
                        break;
                        #endregion
                    }
                case 10:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(300, 50);
                        dolechAnh = new Vector2(-10, -25);
                        break;
                        #endregion
                    }
                case 11:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(246, 52);
                        dolechAnh = new Vector2(-5, -25);
                        break;
                        #endregion
                    }
                case 12:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(528, 90);
                        dolechAnh = new Vector2(-27, -50);
                        break;
                        #endregion
                    }
                case 13:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(492, 94);
                        dolechAnh = new Vector2(-25, -70);
                        break;
                        #endregion
                    }
                case 14:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(768, 94);
                        dolechAnh = new Vector2(-47, -70);
                        break;
                        #endregion
                    }
                case 15:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(492, 86);
                        dolechAnh = new Vector2(-25, -60);
                        break;
                        #endregion
                    }
                default:
                    {
                        #region
                        value = 20;
                        sizeImages = new Vector2(510, 100);
                        break;
                        #endregion
                    }
            }
        } //xử lý các độ lệch ảnh

        public void Dichuyen()
        {
            int SlowDown = 0;
            //hiệu ứng chảy máu theo thời gian
            #region

            #endregion

            //tính vị trí hiện thời
            #region
            index.Y = (int)(position.X) / ((int)GamePage.sizeImageBackGround.X / (int)GamePage.sizeMap.Y);
            index.X = (int)(position.Y) / ((int)GamePage.sizeImageBackGround.Y / (int)GamePage.sizeMap.X);
            if (index.X < 0) index.X = 0;
            if (index.Y < 0) index.Y = 0;
            if (index.X >= GamePage.sizeMap.X) index.X = GamePage.sizeMap.X - 1;
            if (index.Y >= GamePage.sizeMap.Y) index.Y = GamePage.sizeMap.Y - 1;
            #endregion

            //xét việc chuyển hướng đi
            #region
            {
                switch (asset.Huong)
                {
                    case "Top":
                        #region
                        if (Math.Abs(position.Y - (index.X) * (GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X)) <= 10)
                        {
                            if (!map.XacDinhRe(GamePage.level, index, asset.Huong).Equals(asset.Huong) && index != indexRe)
                            {
                                asset.Huong = map.XacDinhRe(GamePage.level, index, asset.Huong);
                                indexRe = index;
                            }
                            else
                                if ((index.X == 0) || GamePage.curMap.getMap()[(int)index.X - 1, (int)index.Y] != 1)
                                {
                                    if (((int)index.Y < GamePage.sizeMap.Y - 1) && (GamePage.curMap.getMap()[(int)index.X, (int)index.Y + 1] == 1))
                                    {
                                        asset.Huong = "Right";
                                    }
                                    else if (((int)index.Y > 0) && (GamePage.curMap.getMap()[(int)index.X, (int)index.Y - 1] == 1))
                                    {
                                        asset.Huong = "Left";
                                    }
                                    else
                                    {
                                        isEnable = false;
                                    }
                                }
                        }
                        #endregion
                        break;
                    case "Left":
                        #region
                        if (Math.Abs(position.X - (index.Y) * (GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y)) <= 10)
                        {
                            if (!map.XacDinhRe(GamePage.level, index, asset.Huong).Equals(asset.Huong) && index != indexRe)
                            {
                                asset.Huong = map.XacDinhRe(GamePage.level, index, asset.Huong);
                                indexRe = index;
                            }
                            else
                                if ((index.Y == 0) || GamePage.curMap.getMap()[(int)index.X, (int)index.Y - 1] != 1)
                                {
                                    if (((int)index.X > 0) && (GamePage.curMap.getMap()[(int)index.X - 1, (int)index.Y] == 1))
                                    {
                                        asset.Huong = "Top";
                                    }
                                    else if (((int)index.X < GamePage.sizeMap.X - 1) && (GamePage.curMap.getMap()[(int)index.X + 1, (int)index.Y] == 1))
                                    {
                                        asset.Huong = "Bottom";
                                    }
                                    else
                                    {
                                        isEnable = false;
                                    }
                                }
                        }
                        #endregion
                        break;
                    case "Right":
                        #region
                        if (!map.XacDinhRe(GamePage.level, index, asset.Huong).Equals(asset.Huong) && index != indexRe)
                        {
                            asset.Huong = map.XacDinhRe(GamePage.level, index, asset.Huong);
                            indexRe = index;
                        }
                        else
                            if ((index.Y == GamePage.sizeMap.Y - 1) || GamePage.curMap.getMap()[(int)index.X, (int)index.Y + 1] != 1)
                            {
                                if (((int)index.X > 0) && (GamePage.curMap.getMap()[(int)index.X - 1, (int)index.Y] == 1))
                                {
                                    asset.Huong = "Top";
                                }
                                else if (((int)index.X < GamePage.sizeMap.X - 1) && (GamePage.curMap.getMap()[(int)index.X + 1, (int)index.Y] == 1))
                                {
                                    asset.Huong = "Bottom";
                                }
                                else
                                {
                                    isEnable = false;
                                }
                            }
                        #endregion
                        break;
                    case "Bottom":
                        #region
                        if (!map.XacDinhRe(GamePage.level, index, asset.Huong).Equals(asset.Huong) && index != indexRe)
                        {
                            asset.Huong = map.XacDinhRe(GamePage.level, index, asset.Huong);
                            indexRe = index;
                        }
                        else
                            if ((index.X == GamePage.sizeMap.X - 1) || GamePage.curMap.getMap()[(int)index.X + 1, (int)index.Y] != 1)
                            {
                                if (((int)index.Y < GamePage.sizeMap.Y - 1) && (GamePage.curMap.getMap()[(int)index.X, (int)index.Y + 1] == 1))
                                {
                                    asset.Huong = "Right";
                                }
                                else if (((int)index.Y > 0) && (GamePage.curMap.getMap()[(int)index.X, (int)index.Y - 1] == 1))
                                {
                                    asset.Huong = "Left";
                                }
                                else
                                {
                                    isEnable = false;
                                }
                            }
                        #endregion
                        break;
                    default: break;
                }
            }
            #endregion

            //dịch chuyển
            #region
            if (asset.Huong.Equals("Bottom"))
            {
                position.Y += (this.asset.Speed - this.asset.Speed * SlowDown / 100) / 2;
            }
            else if (asset.Huong.Equals("Left"))
            {
                position.X -= (this.asset.Speed - this.asset.Speed * SlowDown / 100) / 2;
            }
            else if (asset.Huong.Equals("Top"))
            {
                position.Y -= (this.asset.Speed - this.asset.Speed * SlowDown / 100) / 2;
            }
            else
            {
                position.X += (this.asset.Speed - this.asset.Speed * SlowDown / 100) / 2;
            }
            #endregion
        }

        public void Load(ContentManager cont)
        {
            if (asset.Huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("Images/Character/Enemy/enemy_" + Type.ToString() + "-2");
            }
            else if (asset.Huong.Equals("Top"))
            {
                image = cont.Load<Texture2D>("Images/Character/Enemy/enemy_" + Type.ToString() + "-3");
            }
            else
            {
                image = cont.Load<Texture2D>("Images/Character/Enemy/enemy_" + Type.ToString() + "-1");
            }
            setSizeimages();
        }

        public bool DoiHuong()
        {
            return true;
        }

        public void Draw(SpriteBatch sprite)
        {
            Color curColor = Color.White;
            if (isEnable)
            {
                //sprite.Draw(mauImage, position * scale + DolechMau, new Rectangle(0, 0, (int)(curmau * 99 / Mau), 8),Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                //sprite.Draw(khungmau, position * scale + DolechMau, new Rectangle(0, 0, 101, 8),Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                if (asset.Huong.Equals("Left"))
                {
                    sprite.Draw(image, position + dolechAnh, new Rectangle((int)sizeImages.X / 6 * (count / 5), 0, (int)sizeImages.X / 6, (int)sizeImages.Y),
                        curColor, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);
                }
                else
                {
                    sprite.Draw(image, position + dolechAnh, new Rectangle((int)sizeImages.X / 6 * (count / 5), 0, (int)sizeImages.X / 6, (int)sizeImages.Y),
                                            curColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                }
                if (!GamePage.isPause) count++;
                if (count == 30) count = 0;
            }
        }

        public bool isVaCham(Vector2 posi)
        {
            if (posi.X >= (position.X + 5) && posi.X <= (position.X + size.X - 5) && posi.Y >= (position.Y + 5) && posi.Y <= (position.Y + size.Y - 5))
            {
                return true;
            }
            else return false;
        }
    }
}

